#include "stdafx.h"
#include "ToolWriteLog.h"

#include "ToolBackup.h"
#include "ToolCharType.h"

BOOL SaveLogFile(CString logPath, CString log)
{
	CString logFile = logPath;
	logFile.AppendFormat(_T("%s"), LOG_FILE_NAME);

	CString s = GetCurrentTimeString();
	s.AppendFormat(_T(" %s"), log);

	FILE* file = _tfopen(logFile, _T("a+"));
	if(file == NULL) {
		return FALSE;
	}
    char* s1;
	DWORD size;
	if(!(WCharToMByte(s.GetBuffer(), s1, size)))
	{
		return FALSE;
	}
	int buf_len = LOG_LINE_LENGTH * ((size-1) / LOG_LINE_LENGTH + 1) + 1;
	char* buf = new char[buf_len+1];
	strcpy(buf, s1);
	memset(buf + size - 1, ' ', buf_len - size + 1);
	buf[buf_len - 1] = '\n';
	buf[buf_len] = 0;

	fprintf(file, "%s", buf);
	delete[] s1;
	delete[] buf;
	
	fclose(file);

	return TRUE;
}
